include_guard(GLOBAL)

# protoc -I=$SRC_DIR --grpc_out=$DST_DIR --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` $SRC_DIR/xxx.proto 生成grpc通信接口代码
# protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/xxx.proto 生成请求接口中参数结构的序列化、反序列化代码
message(STATUS "using protoc: ${PROTOBUF_PROTOC_EXECUTABLE}")

find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin)
message(STATUS "using grpc_cpp_plugin: ${GRPC_CPP_PLUGIN}")

function(add_proto_source target name)
    add_custom_command(
        OUTPUT ${PROJECT_BINARY_DIR}/${name}.pb.cc
               ${PROJECT_BINARY_DIR}/${name}.pb.h
        DEPENDS ${PROJECT_SOURCE_DIR}/../thirdparty/proto/${name}.proto
                ${PROTOBUF_PROTOC_EXECUTABLE}
        COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
        ARGS --cpp_out ${PROJECT_BINARY_DIR}
             -I ${PROJECT_SOURCE_DIR}/../thirdparty/proto
             ${PROJECT_SOURCE_DIR}/../thirdparty/proto/${name}.proto
    )
    target_sources(${target}
            PUBLIC ${PROJECT_BINARY_DIR}/${name}.pb.cc
            PUBLIC ${PROJECT_BINARY_DIR}/${name}.pb.h)
endfunction(add_proto_source target name)

function(add_proto_service target name)
    add_custom_command(
        OUTPUT ${PROJECT_BINARY_DIR}/${name}.grpc.pb.cc
               ${PROJECT_BINARY_DIR}/${name}.grpc.pb.h
        DEPENDS ${PROJECT_SOURCE_DIR}/../thirdparty/proto/${name}.proto
                ${PROTOBUF_PROTOC_EXECUTABLE}
                ${GRPC_CPP_PLUGIN}
        COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
        ARGS --grpc_out ${PROJECT_BINARY_DIR}
             -I ${PROJECT_SOURCE_DIR}/../thirdparty/proto
             --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN}
             ${PROJECT_SOURCE_DIR}/../thirdparty/proto/${name}.proto
    )
    target_sources(${target}
            PUBLIC ${PROJECT_BINARY_DIR}/${name}.grpc.pb.cc
            PUBLIC ${PROJECT_BINARY_DIR}/${name}.grpc.pb.h)
endfunction(add_proto_service target name)
